package com.cms.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.common.SiteUtil;
import com.cms.common.StringUtils;
import com.cms.constant.UserCoinConstant;
import com.cms.controller.UserController;
import com.cms.dao.mapper.UserCoinLogMapper;
import com.cms.exception.SystemDBException;
import com.cms.model.TrAdminUser;
import com.cms.model.TrUser;
import com.cms.model.UserCoinLog;
import com.cms.model.UserCommission;
import com.cms.service.MemberUserService;
import com.cms.service.UserCoinLogService;

@Service
public class UserCoinLogServiceImpl implements UserCoinLogService {

	@Autowired
	private UserCoinLogMapper userCoinLogMapper;
	
	@Autowired
	private MemberUserService userService;
	
	@Override
	public void addUserCoinLog(UserCoinLog log) throws SystemDBException {
		   HttpServletRequest req = SiteUtil.getRequest();
		   TrAdminUser tu = SiteUtil.getCurrentUser();
		   log.setActionUid(tu.getId());
		   log.setActionAddr(SiteUtil.getIpAddr(req));
		   userCoinLogMapper.addUserCoinLog(log);		
	}

	@Override
	public void addUserCoinLogBatch(List<UserCoinLog> logs) throws SystemDBException {
		   userCoinLogMapper.addUserCoinLogBatch(logs);
	}

	@Override
	public void handlerUserCash(int status,String remark,Map<String,Object>content) throws Exception {
           UserCoinLog log =new UserCoinLog();
           TrAdminUser tu = SiteUtil.getCurrentUser();
           double coin=StringUtils.str2Double(content.get("amount")+"");
           TrUser user = userService.getUserInfoByUid(StringUtils.obj2String(content.get("uid")));
           if(UserCoinConstant.WITHDRAWAL_SUCCESS==status)
           {
               log.setLiqType(UserCoinConstant.WITHDRAWAL_FROZEN_DEDUCTION);
               log.setRemark(UserCoinConstant.WITHDRAWAL_FROZEN_DEDUCTION_STR);
               log.setCoin(0-coin); 
               log.setUserCoin(user.getCoin());
           }
           else
           {
        	   log.setLiqType(UserCoinConstant.WITHDRAWAL_FROZEN_FAILED);
               log.setRemark(UserCoinConstant.WITHDRAWAL_FROZEN_FAILED_STR);
               log.setCoin(coin);
               log.setUserCoin(user.getCoin()+coin);
           }
           log.setFcoin(0d);
           log.setUid(user.getId());
           log.setUserId(user.getUid());
           log.setCoinOrder(StringUtils.obj2String(content.get("applyOrder")));
           log.setUname(StringUtils.obj2String(content.get("uname")));
           content.put("remark",remark);
           content.put("status",status);
           content.put("userId", tu.getId());
           this.userCoinLogMapper.addUserCoinLog(log);
           this.userCoinLogMapper.hanlderUserCashOrder(content);
           userService.handlerUserCash(status, coin, user);
	}

	@Override
	public void handlerUserRecharge(Map<String, Object> content) throws Exception 
	{
		   UserCoinLog coinLog = new UserCoinLog();
		   TrAdminUser tu = SiteUtil.getCurrentUser();
		   String delete =content.get("is_delete")+"";
		   if("0".equals(delete))
		   {
			   double coin =StringUtils.str2Double(content.get("actualAccount")+"");
			   TrUser user = userService.getUserInfoByUid(StringUtils.obj2String(content.get("uid")));
			   coinLog.setLiqType(UserCoinConstant.RECHARGE);
			   coinLog.setRemark(UserCoinConstant.RECHARGE_STR);
			   coinLog.setUid(user.getId());
			   coinLog.setUserId(user.getUid());
			   coinLog.setUname(user.getLoginName());
			   coinLog.setCoin(coin);
			   coinLog.setFcoin(user.getFcoin());
			   coinLog.setUserCoin(user.getCoin());
			   coinLog.setCoinOrder(StringUtils.obj2String(content.get("chargeOrder")));
			   content.put("userId", tu.getId());
			   this.userCoinLogMapper.addUserCoinLog(coinLog);//添加帐变记录
			   this.userCoinLogMapper.hanlderUserCharge(content);//修改用户充值订单
			   user.setCoin(coin);
			   this.userService.handlerUserCharge(user);
			   return;
		   }
		   throw new Exception("该用户充值订单已经删除或者已经处理，请刷新重试。");
	}
	
	/**
	 * 
	 */
	public void delUserRecharge(Map<String,Object>content)throws Exception
	{
		   this.userCoinLogMapper.delUserRecharge(content);
	}

	@Override
	public void saveUserCommission(UserCommission commission) throws Exception {
		userCoinLogMapper.saveUserCommission(commission);		
	}

	@Override
	public void saveUserCoinLog(Map<String,Object>content) throws Exception {
 		HttpServletRequest request =  SiteUtil.getRequest();
		TrAdminUser user =SiteUtil.getCurrentUser();
		content.put("actionUID", user.getId());
		content.put("actionAddr", SiteUtil.getIpAddr(request));
		content.put("liqType",UserCoinConstant.BETTING_CANCELLATION);
		content.put("remark", UserCoinConstant.BETTING_CANCELLATION_STR);
		userCoinLogMapper.saveUserCoinLogForDeleteComBet(content);
	}
}
